package com.shsubway.activity.manger;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.shsubway.R;
import com.shsubway.base.BaseActivity;
import com.shsubway.model.StaffEntity;
import com.shsubway.model.TestEntity;
import com.shsubway.utils.BigDisMailUtils;
import com.shsubway.utils.Constant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 设置考试
 */
public class CreateTestActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.test_switch)
    Switch testSwitch;
    @BindView(R.id.test_all_score)
    MaterialEditText testAllScore;
    @BindView(R.id.dan_no)
    MaterialEditText danNo;
    @BindView(R.id.pan_no)
    MaterialEditText panNo;
    @BindView(R.id.duo_no)
    MaterialEditText duoNo;
    @BindView(R.id.duo_wrong_score)
    MaterialEditText duoWrongScore;
    @BindView(R.id.btn_over)
    Button btnOver;
    @BindView(R.id.layout_set)
    LinearLayout layoutSet;
    @BindView(R.id.dan_core)
    MaterialEditText danCore;
    @BindView(R.id.pan_score)
    MaterialEditText panScore;
    @BindView(R.id.duo_score)
    MaterialEditText duoScore;
    TestEntity testEntityFromNet = null;
    Intent intent;
    Bundle bundle;
    StaffEntity staffEntity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_test);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        testSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    layoutSet.setVisibility(View.VISIBLE);
                } else {
                    if (buttonView.isPressed()) {
                        showWarron();
                    }
                }
            }
        });

        danCore.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //计算公式
                //   单题个数*单题的分数+多题的个数*多提的分数+判断的个数*判断的分数
                testAllScore.setText((SrtingToInt(duoScore) * SrtingToInt(duoNo) +
                        SrtingToInt(danNo) * SrtingToInt(danCore) +
                        SrtingToInt(panNo) * SrtingToInt(panScore)) + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        duoScore.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //计算公式
                //   单题个数*单题的分数+多题的个数*多提的分数+判断的个数*判断的分数
                testAllScore.setText((SrtingToInt(duoScore) * SrtingToInt(duoNo) +
                        SrtingToInt(danNo) * SrtingToInt(danCore) +
                        SrtingToInt(panNo) * SrtingToInt(panScore)) + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        panScore.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //计算公式
                //   单题个数*单题的分数+多题的个数*多提的分数+判断的个数*判断的分数
                testAllScore.setText((SrtingToInt(duoScore) * SrtingToInt(duoNo) +
                        SrtingToInt(danNo) * SrtingToInt(danCore) +
                        SrtingToInt(panNo) * SrtingToInt(panScore)) + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        danNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //计算公式
                //   单题个数*单题的分数+多题的个数*多提的分数+判断的个数*判断的分数
                testAllScore.setText((SrtingToInt(duoScore) * SrtingToInt(duoNo) +
                        SrtingToInt(danNo) * SrtingToInt(danCore) +
                        SrtingToInt(panNo) * SrtingToInt(panScore)) + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        duoNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //计算公式
                //   单题个数*单题的分数+多题的个数*多提的分数+判断的个数*判断的分数
                testAllScore.setText((SrtingToInt(duoScore) * SrtingToInt(duoNo) +
                        SrtingToInt(danNo) * SrtingToInt(danCore) +
                        SrtingToInt(panNo) * SrtingToInt(panScore)) + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        panNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //计算公式
                //   单题个数*单题的分数+多题的个数*多提的分数+判断的个数*判断的分数
                testAllScore.setText((SrtingToInt(duoScore) * SrtingToInt(duoNo) +
                        SrtingToInt(danNo) * SrtingToInt(danCore) +
                        SrtingToInt(panNo) * SrtingToInt(panScore)) + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void showWarron() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("提示：提交后无法再撤回");
        normalDialog.setMessage("你确定关闭本次测试吗？");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        BmobQuery<TestEntity> testEntityBmobQuery = new BmobQuery<TestEntity>();
                        testEntityFromNet.setOpen(false);
                        testEntityFromNet.update(testEntityFromNet.getObjectId(), new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null) {
                                    layoutSet.setVisibility(View.GONE);
                                    toast("关闭考试成功");
                                    finish();
                                } else {
                                    toast("关闭考试失败");
                                }
                            }
                        });


                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        testSwitch.setChecked(true);
                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();

    }

    private Integer SrtingToInt(MaterialEditText materialEditText) {
        if (TextUtils.isEmpty(materialEditText.getText().toString().trim())) {
            return 0;
        } else {
            return Integer.valueOf(materialEditText.getText().toString().trim());
        }
    }

    private void initView() {
        setTitleBar(toolbar, "考试设置");
        intent=getIntent();
        bundle=intent.getExtras();
        staffEntity= (StaffEntity) bundle.getSerializable(Constant.STAFFENTITY);
        addDate();
    }

    private void addDate() {
        BmobQuery<TestEntity> testEntityBmobQuery = new BmobQuery<>();
        testEntityBmobQuery.addWhereEqualTo("groupId", staffEntity.getGroupId());
        testEntityBmobQuery.findObjects(new FindListener<TestEntity>() {
            @Override
            public void done(List<TestEntity> list, BmobException e) {
                if (e == null) {
                    Log.e("LIS==",list.toString());
                    if (list.size() != 0) {
                        testEntityFromNet = list.get(list.size()-1);
                    Log.e("LISopen==",testEntityFromNet.isOpen()+"");
                        testSwitch.setChecked(testEntityFromNet.isOpen());
                        if (testEntityFromNet.isOpen()) {
                            layoutSet.setVisibility(View.VISIBLE);
                            toast("考试中……");
                            testAllScore.setText(testEntityFromNet.getScoreWrong() + "");
                            danNo.setText(testEntityFromNet.getDanNo() + "");
                            danCore.setText(testEntityFromNet.getDanScore() + "");

                            duoNo.setText(testEntityFromNet.getDuoNo() + "");
                            duoScore.setText(testEntityFromNet.getDuoScore() + "");
                            duoWrongScore.setText(testEntityFromNet.getScoreWrong() + "");

                            panNo.setText(testEntityFromNet.getPanNo() + "");
                            panScore.setText(testEntityFromNet.getPanScore() + "");
                        }
                    }
                } else {
                    toast("获取配置失败，请重试！");
                }
            }
        });
    }

    @OnClick(R.id.btn_over)
    public void onClick() {
        checkNull();
    }

    private void checkNull() {
        if (TextUtils.isEmpty(testAllScore.getText())) {
            testAllScore.setError("总分数不能为空");
        } else {
            if (TextUtils.isEmpty(danNo.getText()) && TextUtils.isEmpty(panNo.getText()) && TextUtils.isEmpty(duoNo.getText())) {
                toast("请至少填写一项题型的数量后进行提交。");
            } else {
                if (SrtingToInt(testAllScore)!=0){
                    final AlertDialog.Builder normalDialog =
                            new AlertDialog.Builder(context);
                    normalDialog.setIcon(R.mipmap.ic_launcher);
                    normalDialog.setTitle("提交后将开启一场新的考试");
                    normalDialog.setMessage("你确定要提交新的测试吗？");
                    normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            TestEntity testEntity = new TestEntity();
                            testEntity.setSroreAll(SrtingToInt(testAllScore));
                            testEntity.setDanNo(SrtingToInt(danNo));
                            testEntity.setDanScore(SrtingToInt(danCore));
                            testEntity.setDuoNo(SrtingToInt(duoNo));
                            testEntity.setDuoScore(SrtingToInt(duoScore));
                            testEntity.setPanNo(SrtingToInt(panNo));
                            testEntity.setPanScore(SrtingToInt(panScore));
                            testEntity.setScoreWrong(SrtingToInt(duoWrongScore));
                            testEntity.setOpen(testSwitch.isChecked());
                            testEntity.setGroupId(staffEntity.getGroupId());
                            testEntity.save(new SaveListener<String>() {
                                @Override
                                public void done(String s, BmobException e) {
                                    if (e == null) {
                                        toast("提交成功");
                                        finish();
                                    } else {
                                        toast("提交失败");
                                    }
                                }
                            });
                        }
                    });
                    normalDialog.setNegativeButton("关闭",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //...To-do

                                    dialog.dismiss();
                                }
                            });
                    // 显示
                    normalDialog.show();

                }else {
                    toast("出题总数请大于0");
                }

            }
        }

    }
}
